package com.xjscrm.console.service.customer.merchat.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.google.common.collect.Lists;
import com.xjscrm.common.annotation.MerId;
import com.xjscrm.console.dto.MerchatChildDeviceRelationDTO;
import com.xjscrm.console.dto.MerchatChildDeviceRelationQueryDTO;
import com.xjscrm.console.entity.MerchatChildDeviceRelation;
import com.xjscrm.console.mapper.MerchatChildDeviceRelationMapper;
import com.xjscrm.console.service.customer.merchat.MerchatChildDeviceRelationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 子账号与机器人中间表(MerchatChildDeviceRelation)表服务实现类
 *
 * @author szl
 * @since 2024-03-12 13:43:21
 */
@Service
public class MerchatChildDeviceRelationServiceImpl implements MerchatChildDeviceRelationService {

    @Autowired
    MerchatChildDeviceRelationMapper merchatChildDeviceRelationMapper;

    @Override
    public List<Long> listRelDeviceUniqueIdsByStaffId(MerchatChildDeviceRelationQueryDTO childDeviceRelation) {

        List<MerchatChildDeviceRelation> deviceRelations = this.merchatChildDeviceRelationMapper.select(childDeviceRelation);
        if (ObjectUtils.isEmpty(deviceRelations)) {
            return Lists.newArrayList();
        }
        return deviceRelations.stream().map(MerchatChildDeviceRelation::getRobotId).distinct().collect(Collectors.toList());
    }

    @Override
    public List<MerchatChildDeviceRelationDTO> list(MerchatChildDeviceRelationQueryDTO childDeviceRelation) {

        List<MerchatChildDeviceRelation> deviceRelations = this.merchatChildDeviceRelationMapper.select(childDeviceRelation);
        if (ObjectUtils.isEmpty(deviceRelations)) {
            return Lists.newArrayList();
        }
        return deviceRelations.stream().map(mapping()).collect(Collectors.toList());
    }

    @Override
    public List<MerchatChildDeviceRelation> listByStaffId(@MerId Long merId, Long staffId) {
        return this.merchatChildDeviceRelationMapper.selectByStaffId(merId, staffId);
    }

    @Override
    public List<Long> listDeviceUniqueIdByStaffId(@MerId Long merId, Long staffId) {
        List<MerchatChildDeviceRelation> merchatChildDeviceRelations = merchatChildDeviceRelationMapper.selectByStaffId(merId, staffId);
        if (org.apache.commons.lang3.ObjectUtils.isEmpty(merchatChildDeviceRelations)) {
            return Lists.newArrayList();
        }
        return merchatChildDeviceRelations.stream().map(MerchatChildDeviceRelation::getRobotId).distinct().collect(Collectors.toList());
    }

    @Override
    public List<Long> listStaffIdsByDeviceUniqueId(@MerId Long merId, Long deviceUniqueId) {
        return merchatChildDeviceRelationMapper.selectStaffIdListByDeviceUniqueId(merId, deviceUniqueId);
    }

    private Function<MerchatChildDeviceRelation, MerchatChildDeviceRelationDTO> mapping() {
        MerchatChildDeviceRelationDTO merchatChildDeviceRelationDTO = new MerchatChildDeviceRelationDTO();
        return item -> {
            BeanUtils.copyProperties(item, merchatChildDeviceRelationDTO);
            return merchatChildDeviceRelationDTO;
        };
    }
}

